<?php
require_once('MySQLdatabase.php');
 
class Student extends MySQLdatabase{
    
    public function createUser($fname,$lname,$username,$userpass,$gender){
        $this->connectDatabase();
        
        $sQuery = "INSERT INTO account(username,password)
                    VALUES('$username',md5('$userpass'))";
        mysql_query($sQuery); //EXECUTE SQL
        
        $row = mysql_fetch_array(mysql_query("SELECT account_id FROM account WHERE username = '$username' AND password = md5('$userpass')"));
        $id = $row['account_id'];
        
        $sQuery = mysql_query("UPDATE account SET stud_id='$id' WHERE account_id = '$id'");
        
        $sQuery = "INSERT INTO student(stud_fname,stud_lname,gender,account_id)
                    VALUES('$fname','$lname','$gender','$id')";
                    
        mysql_query($sQuery); //EXECUTE SQL
    }
     
    public function addStudent($fname,$lname,$mi,$gender){
        $this->connectDatabase();
         
        $sQuery = "INSERT INTO addstudent(stud_lname,stud_fname,stud_mi,stud_gender,log_time)
                    VALUES ('$fname','$lname','$mi','$gender',NOW())";
         
        $result = mysql_query($sQuery); //EXECUTE SQL
    }
     
    public function getStudent(){
        $this->connectDatabase();
        $sQuery = "SELECT stud_id,stud_lname,stud_fname,stud_mi,stud_gender,log_time FROM addstudent ORDER BY stud_lname ASC";
        $result = mysql_query($sQuery);
        $ctr = 0;
         
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            $student[$ctr++] = array('id'=>$row['stud_id'],
                                     'lname'=>$row['stud_lname'],
                                     'fname'=>$row['stud_fname'],
                                     'mi'=>$row['stud_mi'],
                                     'gender'=>$row['stud_gender'],
                                     'time'=>$row['log_time']);
        }
        
        if(!isset($student)){
            $student = null;
        }
        
        return $student;
    }
    
    public function getAdmin(){
        $this->connectDatabase();
        $sQuery = "SELECT stud_lname,stud_fname,gender FROM student ORDER BY stud_lname ASC";
        $result = mysql_query($sQuery);
        $ctr = 0;
         
        while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
            $student[$ctr++] = array('lname'=>$row['stud_lname'],
                                     'fname'=>$row['stud_fname'],
                                     'gender'=>$row['gender']);
        }
        
        if(!isset($student)){
            $student = null;
        }
        
        return $student;
    }
    
    public function updateStudent($id,$fname,$lname,$mi,$gender){
        $this->connectDatabase();
        $sQuery = "UPDATE addstudent SET stud_lname = '$lname', stud_fname = '$fname', stud_mi = '$mi', stud_gender = '$gender'
                   WHERE stud_id = $id";
        $result = mysql_query($sQuery); //EXECUTE SQL
    }
    
    public function deleteStudent($id){
        $this->connectDatabase();
        $sQuery ="DELETE FROM addstudent WHERE stud_id = $id";
        mysql_query($sQuery);
    }
    
    public function deleteAllStudent($id){
        $this->connectDatabase();
        $sQuery ="DELETE FROM addstudent";
        mysql_query($sQuery);
    }
    
    public function checkUserAccount($username,$userpass){
        $this->connectDatabase();
        $sQuery = "SELECT * FROM account WHERE username='$username' AND password=md5('$userpass')";
        $result = mysql_query($sQuery);
        $ctr = 0;
        $row = mysql_fetch_row($result);
        if(!isset($row)){
            $row = null;
        }
        return $row;
    }
}
?>
